package com.hbnu.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbnu.system.mapper.ReportTypeMapper;
import com.hbnu.system.model.entity.Report_Type;
import com.hbnu.system.model.vo.ChildrenNameId;
import com.hbnu.system.model.vo.NameId;
import com.hbnu.system.service.IReportTypeService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ReportTypeServiceImpl extends ServiceImpl<ReportTypeMapper, Report_Type> implements IReportTypeService {
    @Override
    public List<ChildrenNameId> getCascadingData() {
        List<Report_Type> report_types = baseMapper.selectList(Wrappers.lambdaQuery(Report_Type.class));
        HashMap<String, Integer> type_map_2 = new HashMap<>();
        HashMap<String, Integer> type_map_1 = new HashMap<>();
        List<ChildrenNameId> result = new ArrayList<>();

        for (Report_Type report_type : report_types) {
            type_map_2.put(report_type.getReportType2(),report_type.getType2ID());
            type_map_1.put(report_type.getReportType1(),report_type.getType1ID());
        }

        for (Map.Entry<String,Integer> entry : type_map_1.entrySet()) {
            ChildrenNameId childrenNameId = new ChildrenNameId(entry.getKey(), entry.getValue());
            for (Map.Entry<String,Integer> entry1 : type_map_2.entrySet()) {
                if(entry1.getValue()/10 == childrenNameId.getValue()){
                    NameId nameId = new NameId(entry1.getKey(), entry1.getValue());
                    childrenNameId.getChildren().add(nameId);
                }
            }
            result.add(childrenNameId);
        }
        return result;
    }
}
